﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using System.Data.SqlClient;

public partial class Admin_OilUltimateAnalysis : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];
            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }
            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillOilUltimateAnalysis(Convert.ToInt32(id));
                txtOilUltimateAnalysisId.Text = string.Empty;
                trdgOilUltimateAna.Visible = false;
                trTextboxOilUltimate.Visible = true;
            }

            BindDataGrid();
        }
    }
    private void BindDataGrid()
    {
        string ExpID = (string)Session["ExperimentId"];
        if (ExpID != null)
        {
            if (objService.GetOilUltimateAnalysis(ExpID).Tables[0].Rows.Count > 0)
            {

                dgOilUltimateAnal.DataSource = objService.GetOilUltimateAnalysis(ExpID);
                dgOilUltimateAnal.DataBind();
                trdgOilUltimateAna.Visible = true;
                trTextboxOilUltimate.Visible = false;

               
            }
            else
            {
                trdgOilUltimateAna.Visible = false;
                trTextboxOilUltimate.Visible = true;
            }
        }
        else
        {
            trdgOilUltimateAna.Visible = false;
            trTextboxOilUltimate.Visible = true;
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }

    protected void dgOilUltimateAnal_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgOilUltimateAnal.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillOilUltimateAnalysis(id);
        trdgOilUltimateAna.Visible = false;
        trTextboxOilUltimate.Visible = true;
        btnPreviou.Visible = false;
    }

    private void fillOilUltimateAnalysis(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("oil_ultanal", id);

        txtOilUltimateAnalysisId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtC.Text = ds.Tables[0].Rows[0]["C"].ToString();
        txtH.Text = ds.Tables[0].Rows[0]["H"].ToString();
        txtN.Text = ds.Tables[0].Rows[0]["N"].ToString();
        txtO.Text = ds.Tables[0].Rows[0]["O"].ToString();
        txtCl.Text = ds.Tables[0].Rows[0]["Cl"].ToString();
        txtS.Text = ds.Tables[0].Rows[0]["S"].ToString();
        txtCH .Text = ds.Tables[0].Rows[0]["H_C"].ToString();
        txtCO .Text = ds.Tables[0].Rows[0]["O_C"].ToString();
        txtAsh.Text = ds.Tables[0].Rows[0]["Ash"].ToString();
        txtWater .Text = ds.Tables[0].Rows[0]["Water"].ToString();
        txtWaterMethod.Text = ds.Tables[0].Rows[0]["waterMethod"].ToString();
    }

    private void ClearTextBoxes()
    {
        txtOilUltimateAnalysisId.Text = string.Empty ;
        txtC.Text = string.Empty ;
        txtH.Text = string.Empty ;
        txtN.Text = string.Empty ;
        txtO.Text = string.Empty ;
        txtCl.Text = string.Empty ;
        txtS.Text = string.Empty ;
        txtCH .Text = string.Empty ;
        txtCO .Text = string.Empty ;
        txtAsh.Text = string.Empty ;
        txtWater .Text = string.Empty ;
        txtWaterMethod.Text = string.Empty;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (txtOilUltimateAnalysisId.Text != string.Empty)
        {
            if (txtC.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "C", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "C", txtC.Text, txtOilUltimateAnalysisId.Text);
            }

            if (txtH.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "H", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "H", txtH.Text, txtOilUltimateAnalysisId.Text);
            }

            if (txtN.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "N", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "N", txtN.Text, txtOilUltimateAnalysisId.Text);
            }
            if (txtO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O", txtO.Text, txtOilUltimateAnalysisId.Text);
            }
            if (txtCl.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "Cl", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "Cl", txtCl.Text, txtOilUltimateAnalysisId.Text);
            }
            if (txtS.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "S", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "S", txtS.Text, txtOilUltimateAnalysisId.Text);
            }

            if (txtCH.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "H_C", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "H_C", txtCH.Text, txtOilUltimateAnalysisId.Text);
            }
            if (txtC.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "C", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "C", txtC.Text, txtOilUltimateAnalysisId.Text);
            }

            if (txtCO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O_C", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O_C", txtCO.Text, txtOilUltimateAnalysisId.Text);
            }

            if (txtAsh.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "ash", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "ash", txtAsh.Text, txtOilUltimateAnalysisId.Text);
            }

            if (txtWater.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "water", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "water", txtWater.Text, txtOilUltimateAnalysisId.Text);
            }
            if (txtWaterMethod.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "waterMethod", "null", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "waterMethod", txtWaterMethod.Text, txtOilUltimateAnalysisId.Text);
            }


            if (txtO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O_calc", "1", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O_calc", "0", txtOilUltimateAnalysisId.Text);
            }

            if (txtCO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O_C_calc", "1", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "O_C_calc", "0", txtOilUltimateAnalysisId.Text);
            }

            if (txtCH.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "H_C_calc", "1", txtOilUltimateAnalysisId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("oil_ultanal", "H_C_calc", "0", txtOilUltimateAnalysisId.Text);
            }

            BindDataGrid();

            ClearTextBoxes();
        }
        else
        {
            string C_H_calc, O_calc, O_C_calc = string.Empty;


            if (txtO.Text == string.Empty)
            {
                O_calc = "1";
            }
            else
            {
                O_calc = "0";
            }

            if (txtCO.Text == string.Empty)
            {
                O_C_calc = "1";
            }
            else
            {
                O_C_calc = "0";
            }

            if (txtCH.Text == string.Empty)
            {
                C_H_calc = "1";
            }
            else
            {
                C_H_calc = "0";
            }

            if ((string)Session["ExperimentId"] != null)
            {
                string ExpID = (string)Session["ExperimentId"];

                lblMessage.Text = objService.SaveOilUltAnalysis(txtC.Text, txtH.Text, txtN.Text, txtO.Text, txtCl.Text, txtS.Text, txtCH.Text, txtAsh.Text, txtWater.Text, txtWaterMethod.Text, txtCO.Text, O_calc, C_H_calc, O_C_calc,ExpID);

                BindDataGrid();

                ClearTextBoxes();

            }
            else
            {
                string RefeId = (string)Session["ReferenceId"];

                objService.SaveExperiment(null, null , null, null, null, null, null, null, null, null, null, RefeId, null);

                string ExperimentId = objService.GetExperimentId("ref_fk", RefeId);

               // string ExperimentId = objService.GetLastInsertId().ToString();

                lblMessage.Text = objService.SaveOilUltAnalysis(txtC.Text, txtH.Text, txtN.Text, txtO.Text, txtCl.Text, txtS.Text, txtCH.Text, txtAsh.Text, txtWater.Text, txtWaterMethod.Text, txtCO.Text, O_calc, C_H_calc, O_C_calc, ExperimentId);

                Session["ExperimentId"] = ExperimentId;

                BindDataGrid();

                ClearTextBoxes();
            }

            string OilUltimateAnalysisId = objService.GetLastInsertedId("experiment_id", (string)Session["ExperimentId"], "oil_ultanal");
                //GetOilUltimateAnalysisRecentId();

            if ((string)Session["LiquidCompId"] != null)
            {
                string LiquidCompId = (string)Session["LiquidCompId"];

                lblMessage.Text = "Oil ultimate analysis Saved and " + objService.UpdateTable("liquidcomp", "oil_ult_fk", OilUltimateAnalysisId, LiquidCompId);
            }
            else
            {
                lblMessage.Text = "Oil ultimate analysis Saved and " + objService.SaveLiquidComp(null, null, null, OilUltimateAnalysisId, null, null, (string)Session["ExperimentId"]);
                string LiquidCompId = objService.GetLiquidCompId("oil_ult_fk", OilUltimateAnalysisId);
                Session["LiquidCompId"] = LiquidCompId;
            }


        }
    }

    protected void dgOilUltimateAnal_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgOilUltimateAnal.PageIndex = e.NewPageIndex;
        BindDataGrid();
    }
    protected void btnPreviou_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreOilUltimateAnalysis.aspx");
    }
}
